const Koa = require("koa");
const server = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa2-cors");
server.use(cors())
server.use(parser());
const views = require("koa-views");
const nunjucks = require("nunjucks");
server.use(views(__dirname + "/views" , {
    map : {html : "nunjucks"}
}))
const static = require("koa-static");
server.use(static(__dirname + "/public"))

let dataList = ["小小","大大"]

router.get("/" , async ctx => {
    await ctx.render("index")
})

router.get("/fruits" , async ctx => {
    // let fruit = ctx.request.body.fruit;
    // dataList.push(fruit);
    ctx.body = dataList
    console.log(dataList)
    
})
router.post("/fruits" , async ctx => {
    let fruit = ctx.request.body.fruit;
    dataList.push(fruit);
    ctx.body = dataList
    ctx.body = true
    console.log(dataList)
    
})
router.put("/fruits/:id" , async ctx => {
    let id = ctx.params.id;
    let fruit = ctx.request.body.fruit;
    dataList.splice(id,1,fruit);
    ctx.body = dataList
    ctx.body = true
    console.log(dataList)
})
router.delete("/fruits/:id" , async ctx => {
    let id = ctx.params.id;
    // let fruit = ctx.reequest.fruit;
    dataList.splice(id,1);
    ctx.body = dataList
    ctx.body = true
    console.log(dataList)
})




server.use(router.routes());
server.listen(3000 , () => {
    console.log("端口：3000")
})